package com.yfbao.poll.demo.controller;


import com.yfbao.poll.demo.event.PollingEvent;
import com.yfbao.poll.demo.message.TestMessage;
import com.yfbao.poll.demo.service.PollingService;
import com.yfbao.poll.demo.vo.PollVo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController("polling")
public class LongPollResource  {


    @Autowired
    private PollingService pollingService ;

    @Resource
    private ApplicationEventPublisher applicationEventPublisher;



    @PostMapping(value = "/")
    public void listenPoll(HttpServletRequest req, HttpServletResponse resp, PollVo reqVo) {
//        logger.info("listen :"+ reqVo);
        String userName = req.getParameter("user");
        pollingService.doPolling(req,resp,userName,reqVo);
    }


    @PostMapping("message")
    public String listenAndPublish() {
        TestMessage testMessage = new TestMessage("1", "testUser", "nihao");
        applicationEventPublisher.publishEvent(new PollingEvent(testMessage));
        return "ok";
    }

//    @PostMapping("testMessage/{username}/{msgType}")
//    public String listenAndPublishByUser(@PathParam(value = "username")String username,@PathParam(value = "msgType")String msgType) {
//        logger.info("recive req:"+username+","+msgType);
//        TestMessage testMessage = new TestMessage(msgType, username, "nihao");
//        EventDispatcher.fireEvent(new PollingEvent(testMessage));
//        return "ok";
//    }

}
